<template>
  <transition name="slide">
    <music-list :title="title" :bg-image="bgImage" :songs="songs"></music-list>
  </transition>
</template>
<script>
import MusicList from 'components/music-list/music-list'
import { mapGetters } from 'vuex'
import { getSongList } from 'api/recommend'
import { ERR_OK } from 'api/config'
import { getMusicVkey } from 'api/singer'
import { createSong } from 'common/js/song'

export default {
  name: 'disc',
  data () {
    return {
      songs: []
    }
  },
  computed: {
    ...mapGetters([
      'disc'
    ]),
    title () {
      return this.disc.dissname
    },
    bgImage () {
      return this.disc.imgurl
    }
  },
  created () {
    this._getSongList()
  },
  components: {
    MusicList
  },
  methods: {
    parseJsonp (str) {
      return JSON.parse(str.replace(/[_$\w\d]{0,}\(/, '').replace(/\)$/, ''))
    },
    _getSongList () {
      if (!this.disc.dissid) {
        this.$router.push('/recommend')
        return false
      }
      getSongList(this.disc.dissid).then(res => {
      // getSongList(this.$route.params.id).then(res => {
        if (typeof res === 'string') {
          res = this.parseJsonp(res)
        }
        if (res.code === ERR_OK) {
          this.songs = this._normalizeSongs(res.cdlist[0].songlist)
        }
      })
    },
    _normalizeSongs (list) {
      let ret = []
      list.forEach((musicData) => {
        let { songmid } = musicData
        getMusicVkey(songmid).then((res) => {
          let { data: { items: [ { vkey } ] } } = res
          musicData.vkey = vkey
          if (musicData.songid && musicData.albummid) {
            ret.push(createSong(musicData))
          }
        })
      })
      return ret
    }
  }
}
</script>
<style lang="stylus" scoped>
 .slide-enter-active, .slide-leave-active
  transition: all .3s

.slide-enter, .slide-leave-to
  transform: translate3d(100%, 0, 0)
</style>
